package top.rzhsky.controller;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
import top.rzhsky.bean.Message;
import top.rzhsky.bean.Type;
import top.rzhsky.bean.User;
import top.rzhsky.util.AlertUtil;

import java.io.*;
import java.net.Socket;
import java.net.URL;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.ResourceBundle;

/**
 * @author rzh
 * time 2018-11-01 18:48
 */
public class RegisterController implements Initializable {
    @FXML
    private TextField nickName;
    @FXML
    private PasswordField password;
    @FXML
    private DatePicker birthday;
    @FXML
    private ToggleGroup sex;
    @FXML
    private RadioButton man;
    @FXML
    private RadioButton woman;

    private static RegisterController instance;

    public RegisterController() {
        instance = this;
    }

    public static RegisterController getInstance() {
        return instance;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        man.setUserData("男");
        woman.setUserData("女");

        LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
        birthday.setValue(localDateTime.toLocalDate());
    }

    @FXML
    public void registerAction() {
        String nickNameText = nickName.getText();
        String passwordText = password.getText();
        LocalDate birthdayText = birthday.getValue();
        String sexText = (String) sex.getSelectedToggle().getUserData();

        if (nickNameText == null || nickNameText.trim().equals("")) {
            AlertUtil.alert("警告", "账号不能为空", Alert.AlertType.WARNING, ButtonType.OK);
        } else if (passwordText == null || passwordText.trim().equals("")) {
            AlertUtil.alert("警告", "密码不能为空", Alert.AlertType.WARNING, ButtonType.OK);
        }

        Message message = new Message();
        message.setFrom(new User(nickNameText, null, passwordText, birthdayText, sexText));
        message.setType(Type.REGISTER);

        Listener listener = new Listener(message, this);
        new Thread(listener).start();
    }

    @FXML
    public void cancelAction() {
        Stage window = (Stage) nickName.getScene().getWindow();
        window.close();
    }


    public void getAccount(Message message) {
        Platform.runLater(() -> {
            if (message.getFrom().getAccount() != null) {
                AlertUtil.alert("注册成功", "你的账号是" + message.getFrom().getAccount(), Alert.AlertType.CONFIRMATION, ButtonType.OK);
            } else {
                AlertUtil.alert("注册失败", "注册失败", Alert.AlertType.CONFIRMATION, ButtonType.OK);
            }
        });
    }

    public void registerListener(KeyEvent keyEvent) {
        if (keyEvent.getCode() == KeyCode.ENTER) {
            registerAction();
        } else if (keyEvent.getCode() == KeyCode.ESCAPE) {
            cancelAction();
        }
    }
}
